
var App = new Class({
	BC:false,
	name:'App',
	
	initialize:function() {
		this.BC = new Broadcaster();
		this.BC.addListener(this);
		
		var app = this;
		this.loader = new Loader(['collections.loaded', 'projects.loaded'], function () { app.start(); }, this.BC);
		
		this.projects = new Projects(this);
		this.projects.loadList();
			
		this.collections = new Collections(this);
		this.collections.loadList();
	},
	
	start:function() {
		
		this.BC.broadcastMessage('app.start');
	},
	
	events:function(call, data) {
		alert('app.events '+call);
		if(call == 'app.start') {
			this.collections.display();
		}
	}
});


var Projects = new Class({
	list:{},
	name:'projects',
	
	initialize:function(app) {
		this.BC = app.BC;
		
	},
	
	loadList:function() {
		var _parent = this;
		request('getProjects', '', function(res) {
			_parent.list = res;
			_parent.BC.broadcastMessage('projects.loaded');
		});
	},
	
	display:function(id) {
		var data = [];
		this.list.each(function(e) {
			if(e.collection_id == id) {
				this.push({title:e.c_name, header:e.c_name, hasChild:true});
			}
		}, data);
		
		data.push({title:'+ Create New', header:'Manage'});
		data.push({title:'Edit Project'});
		
		var view = Titanium.UI.createTableView({data:data,title:'Projects'}, function(eventObject) {
			switch (eventObject.row) {
				case 0: {
					break;
				}
			}
		});
		
		Titanium.UI.currentWindow.addView(view);
		Titanium.UI.currentWindow.showView(view);
	}
});


var Collections = new Class({
	list:{},
	name:'collections',
	app:{},
	
	initialize:function(app) {
		this.app = app;
		this.BC = app.BC;
		this.BC.addListener(this);
	},
	
	
	loadList:function() {
		$('status').innerHTML = 'collections';
		
		var _parent = this;
		request('collections_load', '', function(res) {
			//_parent.list = res;
			alert('collectionsLoadList');
			
			var str = JSON.encode(res);
			
			
			var Props = Titanium.App.Properties;
			var value = "flooriii";
			Props.setString('objectKey', value)
			alert(Props.getString('objectKey'));
			
			_parent.
			
			_parent.BC.broadcastMessage('collections.loaded');
		});
	},
	
	
	display:function() {
		var data = [];
		
		this.list.each(function(e, i) {
			data.push({title:e.c_name, hasChild:true});
		});
		
		data.push({title: '+ Create New', header:'Manage'});
		
		var _parent = this;
		var view = Titanium.UI.createTableView({data:data, title:'Collection'}, function(eventObject) {
			var row = eventObject.row;
			var count = _parent.list.length;
			
			if(row==count) {
				
			} else {
				// save selected collection id in app properties
				Titanium.App.Properties.setString('collectionID', _parent.list[row].id);
				
				var win = Titanium.UI.createWindow({url:'projects.html',title:'Projects'});
				win.open({animated:true});
			}
		});
		
		Titanium.UI.currentWindow.addView(view);
		Titanium.UI.currentWindow.showView(view);
	}
});




alert = function (str) {
	Titanium.API.log('>>>>>>>>>'+str);
	var dlg = Titanium.UI.createAlertDialog({
	    'title' : 'Alert',
	    'message' : str,
	    'buttonNames' : [ 'Nice' ]
	   });
	   
	 dlg.show();
}

